﻿<%@ WebHandler Language="C#" Class="xinxi_shuiwenliao_peihebi" %>

using System;
using System.Web;
using KeaoBase;
using System.Configuration;
using System.Data;
using System.Collections.Generic;


public class xinxi_shuiwenliao_peihebi : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        try
        {
            string uid = context.Request.Form["uid"];
            string kid = context.Request.Form["kid"];
            if (!Auth.UserLogined(uid, kid))
            {
                context.Response.Write("请重试...");
                return;
            }
            string requestType = context.Request.Form["requestType"];
            if (string.IsNullOrEmpty(requestType))
            {
                context.Response.Write("请重试...");
                return;
            }

            BaseDataBase m_DB = System.Reflection.Assembly.Load("KeaoDataBase").CreateInstance(string.Format("KeaoDataBase.{0}", ConfigurationManager.AppSettings["数据库类型"])) as BaseDataBase;
            if (!m_DB.InitDataBase(ConfigurationManager.ConnectionStrings["DataSource"].ConnectionString))
            {
                context.Response.Write("[]");
                return;
            }
            else
            {

                switch (requestType)
                {
                    case "1":
                        string zhandian = context.Request.Form["zhandian"];
                        string kaishi = context.Request.Form["kaishi"];
                        string createCondition = m_DB.CreateAndCondition("peibileixing", DataValueCommonType.字符串, "水稳料", "", SearchConditionType.等于);
                        createCondition += m_DB.CreateAndCondition("peibiriqi", DataValueCommonType.日期时间, string.Format("{0} 00:00:00", kaishi), string.Format("{0} 23:59:59", kaishi), SearchConditionType.between);
                        if (zhandian != "—全部—")
                        {
                            createCondition += m_DB.CreateAndCondition("zhandian", DataValueCommonType.字符串, zhandian, "", SearchConditionType.等于);
                        }
                        Dictionary<string, string> orderBys = new Dictionary<string, string>();
                        orderBys.Add("peibiriqi", "asc");
                        orderBys.Add("gongcheng", "asc");
                        orderBys.Add("peibiwuliao", "asc");
                        DataTable Dt = m_DB.GetDataTableByCondition("tbl_peihebi", createCondition,orderBys);
                        string responseJson = KeaoUtils.Dt2Json(Dt);
                        context.Response.Write(responseJson);
                        break;
                    case "2":
                        string peibizhandian = context.Request.Form["zhandian"];
                        string peibiriqi = context.Request.Form["peibiriqi"];
                        string gongcheng = context.Request.Form["gongcheng"];
                        string peibi = context.Request.Form["peibi"];
                        string peibiid = context.Request.Form["peibiid"];
                        string guanlianwuliao = context.Request.Form["guanlianwuliao"];
                        string guanlianbianhao = context.Request.Form["guanlianbianhao"];
                        string peibicailiao = guanlianwuliao;
                        if (guanlianwuliao.Contains("("))
                        {
                            peibicailiao = guanlianwuliao.Substring(0, guanlianwuliao.LastIndexOf('('));
                        }
                        if (string.IsNullOrEmpty(peibiid))
                        {
                            //新增
                            TableRow newRow = new TableRow("tbl_peihebi", Guid.NewGuid().ToString(), DBCtl.插入);
                            TableCol col1 = new TableCol() { ColName = "gongcheng", ValueMember = gongcheng, ValueType = DataValueCommonType.字符串 };
                            TableCol col2 = new TableCol() { ColName = "zhandian", ValueMember = peibizhandian, ValueType = DataValueCommonType.字符串 };
                            TableCol col3 = new TableCol() { ColName = "peibiriqi", ValueMember = peibiriqi, ValueType = DataValueCommonType.日期时间 };
                            TableCol col4 = new TableCol() { ColName = "peibiwuliao", ValueMember = peibicailiao, ValueType = DataValueCommonType.字符串 };
                            TableCol col5 = new TableCol() { ColName = "peibi", ValueMember = peibi, ValueType = DataValueCommonType.数字 };
                            TableCol col6 = new TableCol() { ColName = "kehubianhao", ValueMember = ConfigurationManager.AppSettings["客户编号"], ValueType = DataValueCommonType.字符串 };
                            TableCol col7 = new TableCol() { ColName = "shangchuanzhuangtai", ValueMember = '1', ValueType = DataValueCommonType.字符串 };
                            TableCol col8 = new TableCol() { ColName="peibileixing", ValueMember="水稳料", ValueType= DataValueCommonType.字符串 };
                            TableCol col9 = new TableCol() { ColName = "guanlianwuliao", ValueMember = guanlianwuliao, ValueType = DataValueCommonType.字符串 };
                            TableCol col10 = new TableCol() { ColName = "guanlianbianhao",ValueMember = guanlianbianhao,ValueType = DataValueCommonType.字符串 };
                            newRow.DataCols.Add(col1);
                            newRow.DataCols.Add(col2);
                            newRow.DataCols.Add(col3);
                            newRow.DataCols.Add(col4);
                            newRow.DataCols.Add(col5);
                            newRow.DataCols.Add(col6);
                            newRow.DataCols.Add(col7);
                            newRow.DataCols.Add(col8);
                            newRow.DataCols.Add(col9);
                            newRow.DataCols.Add(col10);
                            if (m_DB.SaveTableRows(newRow) > 0)
                            {
                                context.Response.Write("OK");
                            }
                            else
                            {
                                context.Response.Write("Failed");
                            }
                        }
                        else
                        {
                            //修改
                            TableRow eidtRow = new TableRow("tbl_peihebi", peibiid, DBCtl.修改);
                            TableCol col1 = new TableCol() { ColName = "gongcheng", ValueMember = gongcheng, ValueType = DataValueCommonType.字符串 };
                            TableCol col2 = new TableCol() { ColName = "zhandian", ValueMember = peibizhandian, ValueType = DataValueCommonType.字符串 };
                            TableCol col3 = new TableCol() { ColName = "peibiriqi", ValueMember = peibiriqi, ValueType = DataValueCommonType.日期时间 };
                            TableCol col4 = new TableCol() { ColName = "peibiwuliao", ValueMember = peibicailiao, ValueType = DataValueCommonType.字符串 };
                            TableCol col5 = new TableCol() { ColName = "peibi", ValueMember = peibi, ValueType = DataValueCommonType.数字 };
                            TableCol col6 = new TableCol() { ColName = "guanlianwuliao", ValueMember = guanlianwuliao, ValueType = DataValueCommonType.字符串 };
                            TableCol col7 = new TableCol() { ColName = "guanlianbianhao", ValueMember = guanlianbianhao, ValueType = DataValueCommonType.字符串 };
                            eidtRow.DataCols.Add(col1);
                            eidtRow.DataCols.Add(col2);
                            eidtRow.DataCols.Add(col3);
                            eidtRow.DataCols.Add(col4);
                            eidtRow.DataCols.Add(col5);
                            eidtRow.DataCols.Add(col6);
                            eidtRow.DataCols.Add(col7);
                            if (m_DB.SaveTableRows(eidtRow) > 0)
                            {
                                context.Response.Write("OK");
                            }
                            else
                            {
                                context.Response.Write("Failed");
                            }
                        }

                        break;
                    case "3":
                        string delID = context.Request.Form["peibiid"];
                        TableRow delRow = new TableRow() { ID = delID, TableName = "tbl_peihebi" };
                        if (m_DB.DeleteRowByID(delRow) > 0)
                        {
                            context.Response.Write("OK");
                        }
                        else
                        {
                            context.Response.Write("Failed");
                        }
                        break;
                }
                if (requestType == "4")
                {
                    string zhandian = context.Request.Form["zhandian"];
                    string peibiriqi = context.Request.Form["peibiriqi"];
                    string gongcheng = context.Request.Form["gongcheng"];
                    string peibidan = context.Request.Form["peibidan"];
                    string createCondition = m_DB.CreateAndCondition("zhandian", DataValueCommonType.字符串, zhandian, "", SearchConditionType.等于);
                    createCondition += m_DB.CreateAndCondition("mingcheng", DataValueCommonType.字符串, peibidan, "", SearchConditionType.等于);
                    DataTable DtPeiBiDan = m_DB.GetDataTableByCondition("tbl_peibidan", createCondition);
                    foreach (DataRow row in DtPeiBiDan.Rows)
                    {
                        TableRow newRow = new TableRow("tbl_peihebi", Guid.NewGuid().ToString(), DBCtl.插入);
                        newRow.DataCols.Add(new TableCol() { ColName = "peibiriqi", ValueMember = peibiriqi, ValueType = DataValueCommonType.日期时间 });
                        newRow.DataCols.Add(new TableCol() { ColName = "peibileixing", ValueMember = "水稳料", ValueType = DataValueCommonType.字符串 });
                        newRow.DataCols.Add(new TableCol() { ColName = "gongcheng", ValueMember = gongcheng, ValueType = DataValueCommonType.字符串 });
                        newRow.DataCols.Add(new TableCol() { ColName = "peibiwuliao", ValueMember = row["peibiwuliao"], ValueType = DataValueCommonType.字符串 });
                        newRow.DataCols.Add(new TableCol() { ColName = "peibi", ValueMember = row["peibi"], ValueType = DataValueCommonType.字符串 });
                        newRow.DataCols.Add(new TableCol() { ColName = "zhandian", ValueMember = zhandian, ValueType = DataValueCommonType.字符串 });
                        newRow.DataCols.Add(new TableCol() { ColName = "kehubianhao", ValueMember = ConfigurationManager.AppSettings["客户编号"], ValueType = DataValueCommonType.字符串 });
                        newRow.DataCols.Add(new TableCol() { ColName = "shangchuanzhuangtai", ValueMember = "1", ValueType = DataValueCommonType.字符串 });
                        newRow.DataCols.Add(new TableCol() { ColName = "guanlianwuliao", ValueMember = row["guanlianwuliao"], ValueType = DataValueCommonType.字符串 });
                        newRow.DataCols.Add(new TableCol() { ColName = "guanlianbianhao", ValueMember = row["guanlianbianhao"], ValueType = DataValueCommonType.字符串 });
                        m_DB.SaveTableRows(newRow);
                    }
                    context.Response.Write("OK");
                }
            }
        }
        catch (Exception err)
        {
            KeaoUtils.LogTxt(string.Format("水稳料配比异常。{0}", err.Message));
        }

    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}